/**   
* @Title: CompanyServiceImpl.java 
* @Package com.nebula.upms.service.impl 
* @Description: TODO(用一句话描述该文件做什么) 
* @author zhanghui   
* @date 2016年2月17日 上午10:08:19 
* @version V1.0   
*/
package com.nebula.upms.service.biz.impl;

import java.util.Date;
import java.util.List;

import com.nebula.common.util.UUIDGenerator;
import com.nebula.upms.service.biz.CompanyService;
import com.nebula.upms.service.model.po.UpmsCompanyPO;
import com.nebula.upms.service.model.po.UpmsCompanyPOExample;
import com.nebula.upms.service.model.po.UpmsDepartmentPO;
import com.nebula.upms.service.model.po.UpmsDepartmentPOExample;
import com.nebula.upms.service.support.constant.UpmsLocalFinal;
import com.nebula.upms.service.support.datasource.UpmsDs;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.nebula.common.framework.BaseService;


/**
 * @ClassName: CompanyServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author zhanghui
 * @date 2016年2月17日 上午10:08:19
 * 
 */
@Service("companyService")
@Transactional
public class CompanyServiceImpl extends BaseService implements CompanyService {
	@Override
	public String companyFullTreeJson() {
		// root

		ObjectMapper mapper = new ObjectMapper();
		ObjectNode rootNode = mapper.createObjectNode();
		rootNode.put("id", "ROOT");
		rootNode.put("text", "组织根节点");
		rootNode.put("iconCls", "fa fa-th-large");
		rootNode.put("priority", 0);
		rootNode.put("type", "ROOT");
		ArrayNode rootArrayNode = mapper.createArrayNode();

		UpmsCompanyPOExample example1 = new UpmsCompanyPOExample();
		example1.createCriteria().andIsDeleteEqualTo(false);
		example1.setOrderByClause(" PRIORITY ASC ");
		List<UpmsCompanyPO> fullCompanyList = UpmsDs.instance().mybatis().ds().mapper().selectByExample(example1);
		for (UpmsCompanyPO UpmsCompanyPO : fullCompanyList) {
			ObjectNode cmpNode = mapper.createObjectNode();
			cmpNode.put("id", UpmsCompanyPO.getCompanyId());
			cmpNode.put("text", UpmsCompanyPO.getName());
			cmpNode.put("iconCls", "fa " + UpmsCompanyPO.getIcon());
			cmpNode.put("state", "open");
			cmpNode.put("priority", UpmsCompanyPO.getPriority());
			cmpNode.put("type", "COMPANY");
			ArrayNode cmpArrayNode = mapper.createArrayNode();
			UpmsDepartmentPOExample example2 = new UpmsDepartmentPOExample();
			example2.createCriteria().andCompanyIdEqualTo(UpmsCompanyPO.getCompanyId()).andIsDeleteEqualTo(false);
			example2.setOrderByClause(" PRIORITY ASC ");
			List<UpmsDepartmentPO> departmentList = UpmsDs.instance().mybatis().ds().mapper().selectByExample(example2);

			for (UpmsDepartmentPO UpmsDepartmentPO : departmentList) {
				ObjectNode depNode = mapper.createObjectNode();
				depNode.put("id", UpmsDepartmentPO.getDepartmentId());
				depNode.put("text", UpmsDepartmentPO.getName());
				depNode.put("iconCls", "fa " + UpmsDepartmentPO.getIcon());
				depNode.put("state", "open");
				depNode.put("priority", UpmsDepartmentPO.getPriority());
				depNode.put("type", "DEPARTMENT");
				cmpArrayNode.add(depNode);
			}
			cmpNode.put("children", cmpArrayNode);
			rootArrayNode.add(cmpNode);
		}
		rootNode.put("children", rootArrayNode);
		ArrayNode baseArrayNode = mapper.createArrayNode();
		baseArrayNode.add(rootNode);

		String json = "";
		try {
			json = mapper.writeValueAsString(baseArrayNode);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return json;
	}

	/*
	 * <p>Title: getUpmsDepartmentPOByKey</p> <p>Description: </p>
	 * 
	 * @param departmentId
	 * 
	 * @return
	 * 
	 * @see
	 * com.nebula.upms.service.CompanyService#getUpmsDepartmentPOByKey(java.lang.
	 * String)
	 */
	@Override
	public UpmsDepartmentPO getUpmsDepartmentPOByKey(String departmentId) {
		// TODO Auto-generated method stub
		return UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsDepartmentPO.class, departmentId);
	}

	/*
	 * <p>Title: getUpmsCompanyPOByKey</p> <p>Description: </p>
	 * 
	 * @param companyId
	 * 
	 * @return
	 * 
	 * @see
	 * com.nebula.upms.service.CompanyService#getUpmsCompanyPOByKey(java.lang.
	 * String)
	 */
	@Override
	public UpmsCompanyPO getUpmsCompanyPOByKey(String companyId) {
		// TODO Auto-generated method stub
		return UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsCompanyPO.class, companyId);
	}

	/*
	 * <p>Title: saveCompany</p> <p>Description: </p>
	 * 
	 * @param UpmsCompanyPO
	 * 
	 * @see
	 * com.nebula.upms.service.CompanyService#saveCompany(com.hbdiy.common.
	 * model.po.UpmsCompanyPO)
	 */
	@Override
	public int saveCompany(UpmsCompanyPO UpmsCompanyPO) {
		UpmsCompanyPO.setCompanyId(UUIDGenerator.random32UUID());
		UpmsCompanyPO.setType(UpmsLocalFinal.COMPANY_TYPE_Y31);
		UpmsCompanyPO.setCreateTime(new Date().getTime());
		UpmsCompanyPO.setIsDelete(false);

		int priority = 0;
		UpmsCompanyPOExample example1 = new UpmsCompanyPOExample();
		example1.setOrderByClause("PRIORITY DESC");
		UpmsCompanyPO last = UpmsDs.instance().mybatis().ds().mapper().selectFirstByExample(example1);
		if (null != last) {
			priority = last.getPriority() + 1;
		}
		UpmsCompanyPO.setPriority(priority);
		int count = UpmsDs.instance().mybatis().ds().mapper().insert(UpmsCompanyPO);
		return count;
	}

	@Override
	public int updateCompany(UpmsCompanyPO UpmsCompanyPO) {
		UpmsCompanyPO.setUpdateTime(new Date().getTime());
		int count = UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(UpmsCompanyPO);
		return count;
	}

	/*
	 * <p>Title: removeCompany</p> <p>Description: </p>
	 * 
	 * @param companyId
	 * 
	 * @see
	 * com.nebula.upms.service.CompanyService#removeCompany(java.lang.String)
	 */
	@Override
	public int removeCompany(String companyId) {
		UpmsCompanyPO UpmsCompanyPO = UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsCompanyPO.class, companyId);
		UpmsDepartmentPOExample example1 = new UpmsDepartmentPOExample();
		example1.createCriteria().andCompanyIdEqualTo(companyId).andIsDeleteEqualTo(false);

		int depCount =(int) UpmsDs.instance().mybatis().ds().mapper().countByExample(example1);
		if (depCount < 1) {
			UpmsCompanyPO.setIsDelete(true);
			int count = UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(UpmsCompanyPO);
			return count;
		} else {
			return 0;
		}
		// int
		// count=UpmsDs.instance().mybatis().ds().mapper().deleteByPrimaryKey(UpmsCompanyPO.class,
		// companyId);
	}

	/*
	 * <p>Title: moveup</p> <p>Description: </p>
	 * 
	 * @param companyId
	 * 
	 * @return
	 * 
	 * @see com.nebula.upms.service.CompanyService#moveup(java.lang.String)
	 */
	@Override
	public int moveup(String companyId) {
		UpmsCompanyPO UpmsCompanyPO = UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsCompanyPO.class, companyId);

		UpmsCompanyPOExample example1 = new UpmsCompanyPOExample();
		example1.createCriteria().andIsDeleteEqualTo(false);
		example1.setOrderByClause(" PRIORITY ASC");
		List<UpmsCompanyPO> list = UpmsDs.instance().mybatis().ds().mapper().selectByExample(example1);
		int index = 0;
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getCompanyId().equals(companyId)) {
				if (i == 0) {
					index = 0;
				} else {
					index = i - 1;
				}
				break;
			}
		}

		UpmsCompanyPO prev = list.get(index);
		int priority = prev.getPriority();
		prev.setPriority(UpmsCompanyPO.getPriority());
		UpmsCompanyPO.setPriority(priority);
		UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(prev);
		UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(UpmsCompanyPO);
		return 1;
	}

	/*
	 * <p>Title: movedown</p> <p>Description: </p>
	 * 
	 * @param companyId
	 * 
	 * @return
	 * 
	 * @see com.nebula.upms.service.CompanyService#movedown(java.lang.String)
	 */
	@Override
	public int movedown(String companyId) {
		UpmsCompanyPO UpmsCompanyPO = UpmsDs.instance().mybatis().ds().mapper().selectByPrimaryKey(UpmsCompanyPO.class, companyId);

		UpmsCompanyPOExample example1 = new UpmsCompanyPOExample();
		example1.createCriteria().andIsDeleteEqualTo(false);
		example1.setOrderByClause(" PRIORITY ASC");
		List<UpmsCompanyPO> list = UpmsDs.instance().mybatis().ds().mapper().selectByExample(example1);
		int index = 0;
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getCompanyId().equals(companyId)) {
				if (i == (list.size() - 1)) {
					index = list.size() - 1;
				} else {
					index = i + 1;
				}
				break;
			}
		}

		UpmsCompanyPO next = list.get(index);
		int priority = next.getPriority();
		next.setPriority(UpmsCompanyPO.getPriority());
		UpmsCompanyPO.setPriority(priority);
		UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(next);
		UpmsDs.instance().mybatis().ds().mapper().updateByPrimaryKeySelective(UpmsCompanyPO);
		return 1;
	}
}
